package com.hs.android.fortress.intro;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Process;
import com.google.gson.Gson;
import com.hs.android.fortress.model.Player;
import com.hs.android.fortress.net.AsyncNetworking;
import com.hs.android.fortress.net.FormPostCommand;
import com.hs.android.fortress.net.NetworkCommand;
import com.hs.android.fortress.net.ParamsMaker;
import com.hs.android.fortress.net.AsyncNetworking.onNetworkProcessListener;
import com.hs.android.fortress.GameApplication;
import com.hs.android.fortress.R;

public class IntroActivity extends Activity {

	private static final String TAG = IntroActivity.class.getSimpleName();

	private String user_id, user_pw;

	private NetworkCommand mCommand;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);

		SharedPreferences pref = getSharedPreferences("client", MODE_PRIVATE);
		user_id = pref.getString("id", null);
		user_pw = pref.getString("pw", null);

		if (user_id != null && user_pw != null) {
			String loginPath = "/member/login";

			mCommand = new FormPostCommand(loginPath, ParamsMaker.makeIdPwParams(user_id, user_pw));
			
			AsyncNetworking.newInstance(this, mCommand, mListener).execute();
			
		} else {
			FragmentManager fm = getFragmentManager();
			loginFragment fragment = (loginFragment) fm.findFragmentById(R.id.intro_container);
			if (fragment == null) {
				fragment = new loginFragment();
				fm.beginTransaction().add(R.id.intro_container, fragment).commit();
			}
		}

	}
	
	private onNetworkProcessListener mListener = new onNetworkProcessListener() {

		@Override
		public void onPostExecute(String result) {
			
			if (result == null) {
				AsyncNetworking.makeNetworkProblemDialog(IntroActivity.this)
				.setPositiveButton("Try Again", mDialogListener)
				.setNegativeButton("Exit Game", mDialogListener)
				.create().show();
			}else{
				Gson gson = new Gson();
				Player user = gson.fromJson(result, Player.class);

				GameApplication app = (GameApplication) IntroActivity.this.getApplication();
				app.setMyPlayer(user);
				 
				IntroIntentFactory factory = new IntroIntentFactory(IntroActivity.this);
				Intent intent = factory.create(user);				
				startActivity(intent);
				finish();
			}
		}
	};
	
	public OnClickListener mDialogListener = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			
			if(which == dialog.BUTTON_POSITIVE){
				AsyncNetworking.newInstance(IntroActivity.this, mCommand, mListener).execute();
			}else if(which == dialog.BUTTON_NEGATIVE){
				Process.killProcess(Process.myPid());
			}
		}
	};
	

}
